package com.pet.client.role;

import java.io.File;
import java.util.Date;
import java.util.List;

import com.pet.framework.constant.ATTFrameWork;
import com.pet.framework.page.ATTPage;
import com.pet.framework.page.Page;
import com.pet.framework.page.PagerFactory;
import com.pet.framework.util.BaseAction;
import com.pet.framework.util.JsonUtil;
import com.pet.framework.util.MD5Util;
import com.pet.framework.util.StringUtil;
import com.pet.server.bo.role.RoleService;
import com.pet.server.po.AuthInfo;
import com.pet.server.po.CampaignPosition;
import com.pet.server.po.Constant;
import com.pet.server.po.ConstantUi;
import com.pet.server.po.MenuInfo;
import com.pet.server.po.PetSay;
import com.pet.server.po.RoleInfo;
import com.pet.server.po.SysUser;

public class RoleAction extends BaseAction {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private RoleService roleService;
    private Integer id;
    private String href,name,visitility,desc,data,display,contsName,pcode,val,imgFileFileName;
    private Integer pid,seq,sort;
    private AuthInfo auth;
    private RoleInfo role;
    private Integer[] ids;
    private SysUser user;
    private File imgFile;
    private ConstantUi cu;
    private CampaignPosition cp;
    
   
    public CampaignPosition getCp() {
		return cp;
	}
	public void setCp(CampaignPosition cp) {
		this.cp = cp;
	}
	public ConstantUi getCu() {
		return cu;
	}
	public void setCu(ConstantUi cu) {
		this.cu = cu;
	}
	public File getImgFile() {
		return imgFile;
	}
	public void setImgFile(File imgFile) {
		this.imgFile = imgFile;
	}
    
	public SysUser getUser() {
		return user;
	}
	public void setUser(SysUser user) {
		this.user = user;
	}
	public String getDisplay() {
		return display;
	}
	public void setDisplay(String display) {
		this.display = display;
	}
	public String getContsName() {
		return contsName;
	}
	public void setContsName(String contsName) {
		this.contsName = contsName;
	}
	public Integer getSort() {
		return sort;
	}
	public void setSort(Integer sort) {
		this.sort = sort;
	}
	public String getPcode() {
		return pcode;
	}
	public void setPcode(String pcode) {
		this.pcode = pcode;
	}
	public String getVal() {
		return val;
	}
	public void setVal(String val) {
		this.val = val;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public String getData() {
		return data;
	}
	public void setData(String data) {
		this.data = data;
	}
	public RoleInfo getRole() {
		return role;
	}
	public void setRole(RoleInfo role) {
		this.role = role;
	}
	public AuthInfo getAuth() {
		return auth;
	}
	public void setAuth(AuthInfo auth) {
		this.auth = auth;
	}
	public RoleService getRoleService() {
		return roleService;
	}
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getHref() {
		return href;
	}
	public void setHref(String href) {
		this.href = href;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getPid() {
		return pid;
	}
	public void setPid(Integer pid) {
		this.pid = pid;
	}
	public Integer getSeq() {
		return seq;
	}
	public void setSeq(Integer seq) {
		this.seq = seq;
	}
	public String getVisitility() {
		return visitility;
	}
	public void setVisitility(String visitility) {
		this.visitility = visitility;
	}
	public String getDesc() {
		return desc;
	}
	public void setDesc(String desc) {
		this.desc = desc;
	}
	
	public String getImgFileFileName() {
		return imgFileFileName;
	}
	public void setImgFileFileName(String imgFileFileName) {
		this.imgFileFileName = imgFileFileName;
	}
	
	
	/**
	 * 菜单管理
	 * @return
	 */
	public String menuManager(){
		List<MenuInfo> list = roleService.queryRootMenu();
		setAttr("clist", list);
		return SUCCESS;
	}
	private void s(List<MenuInfo> l){
		System.out.println(l.size());
		for(MenuInfo m:l){
			System.out.println(m.getPId()+" "+m.getMenuName());
			s(m.getList());
			
		}
	}
	
	public String toMenu(){
		MenuInfo menu = null;
		if(!StringUtil.isEmpty(id)){
			menu = roleService.getObject(id, MenuInfo.class);
		}
		setAttr(ATTFrameWork.MENU_INFO, menu);
		return SUCCESS;
	}
	
	public String suMenu(){
		MenuInfo menu = null;
		if(!StringUtil.isEmpty(id)){
			menu = roleService.getObject(id, MenuInfo.class);
		}
		if(menu!=null){
			roleService.updateMenuInfo(menu, href, name, pid, seq, visitility, desc);
		}else{
			roleService.saveMenuInfo(href, name, pid, seq, visitility, desc);
		}
		return SUCCESS;
	}
	/**权限管理*/
	public String authManager(){
		List<AuthInfo> list = roleService.queryRootAuth();
		setAttr("clist", list);
		return SUCCESS;
	}
	public String toAuth(){
		AuthInfo ai = null;
		if(!StringUtil.isEmpty(id)){
			ai = roleService.getObject(id, AuthInfo.class);
		}
		setAttr(ATTFrameWork.AUTH_INFO, ai);
		return SUCCESS;
	}
	public String suAuth(){
		AuthInfo ai = null;
		if(!StringUtil.isEmpty(id)){
			ai = roleService.getObject(id, AuthInfo.class);
		}
		if(ai!=null && auth!=null){
			roleService.updateAuthInfo(ai, auth);
		}else if(auth!=null){
			roleService.saveAuthInfo(auth);
		}
		return SUCCESS;
	}
	//角色管理
	public String roleManager(){
		List<RoleInfo> list = roleService.queryRoleInfo();
		setAttr("clist", list);
		return SUCCESS;
	}
	public String toRole(){
		RoleInfo role=null;
		if(!StringUtil.isEmpty(id)){
			role = roleService.getObject(id, RoleInfo.class);
		}
		setAttr(ATTFrameWork.ROLE_INFO, role);
		return SUCCESS;
	}
	public String suRole(){
		RoleInfo r=null;
		if(!StringUtil.isEmpty(id)){
			r = roleService.getObject(id, RoleInfo.class);
		}
		if(r!=null&&role!=null){
			roleService.updateRoleInfo(r, role);
		}else if(role!=null){
			roleService.saveObject(role);
		}
		return SUCCESS;
	}
	/**角色菜单*/
	public String roleMenu(){
		List<RoleInfo> list = roleService.queryRoleInfo();
		List<MenuInfo> menuList = roleService.queryRootMenu();
		setAttr(ATTFrameWork.LIST, list);
		setAttr("clist", menuList);
		return SUCCESS;
	}
	/**设置角色菜单*/
	public String loadMenuByRole(){
		if(!StringUtil.isEmpty(id)){//职位id
			List<MenuInfo> list = roleService.queryMenuInfoByRoleId(null,id);
			setData(JsonUtil.createCheckMenu(list).toString());
		}else{
			setData("{\"menuReg\":\"\"}");
		}
		return SUCCESS;
	}
	public String setMenuByRole(){
		if(!StringUtil.isEmpty(id) && !StringUtil.isEmpty(ids)){
			roleService.saveForSetMenuByRole(id, ids);
		}
		return SUCCESS;
	}
	/**角色权限*/
	public String roleAuth(){
		List<RoleInfo> list = roleService.queryRoleInfo();
		List<AuthInfo> authList = roleService.queryRootAuth();
		setAttr(ATTFrameWork.LIST, list);
		setAttr("clist", authList);
		return SUCCESS;
	}
	/**设置角色权限*/
	public String loadAuthByRole(){
		if(!StringUtil.isEmpty(id)){//职位id
			List<AuthInfo> list = roleService.queryAuthInfoByRoleId(id);
			setData(JsonUtil.createAuthInfo(list).toString());
		}else{
			setData("{\"menuReg\":\"\"}");
		}
		return SUCCESS;
	}
	public String setAuthByRole(){
		if(!StringUtil.isEmpty(id) && !StringUtil.isEmpty(ids)){
			roleService.saveForSetAuthByRole(id, ids);
		}
		return SUCCESS;
	}
	//常量管理
	public String toConts(){
		Constant conts = null;
		if(!StringUtil.isEmpty(id)){
			conts = roleService.getObject(id, Constant.class);
		}
		super.setAttr(ATTFrameWork.CONSTANTS, conts);
		return SUCCESS;
	}
	public String constantsList(){
		Page page = PagerFactory.createPage();
		roleService.searchConstants(name,StringUtil.dealNull(pcode, ATTFrameWork.PCODE), display, page);
		setAttr(ATTPage.PAGE_LIST,page);
		return SUCCESS;
	}
	//常量保存修改
	public String suConts(){
		Constant conts = null;
		if(!StringUtil.isEmpty(id)){
			conts = roleService.getObject(id, Constant.class);
		}
		setPcode(StringUtil.dealNull(pcode, ATTFrameWork.PCODE));
		if(conts!=null){
			roleService.updateConstant(display, contsName, sort, val, conts,imgFile,imgFileFileName);
		}else{
			roleService.saveConstant(display, pcode, contsName, sort, val,imgFile,imgFileFileName);
		}
		return SUCCESS;
	}
	
	//活动位置常量管理
			public String toCampaignPosition(){
				CampaignPosition camPos = null;
				if(!StringUtil.isEmpty(id)){
					camPos = roleService.getObject(id, CampaignPosition.class);
				}
				super.setAttr(ATTFrameWork.COMPOS, camPos);
				return SUCCESS;
			}
			//活动位置常量列表
			public String campaignPositionList(){
				Page page = PagerFactory.createPage();
				roleService.searchCampaignPosition(name,page);
				setAttr(ATTPage.PAGE_LIST,page);
				return SUCCESS;
			}
			//活动位置保存修改
			public String suCampaignPosition(){
				CampaignPosition camPos = null;
				if(!StringUtil.isEmpty(id)){
					camPos = roleService.getObject(id, CampaignPosition.class);
				}
				
				if(camPos!=null){
					roleService.updateCampaignPosition(cp, camPos);
				}else{
					roleService.saveCampaignPosition(cp);
				}
				return SUCCESS;
			}
	
		//UI常量管理
		public String toUiConts(){
			ConstantUi contsUi = null;
			if(!StringUtil.isEmpty(id)){
				contsUi = roleService.getObject(id, ConstantUi.class);
			}
			super.setAttr(ATTFrameWork.CONSTANTSUI, contsUi);
			return SUCCESS;
		}
		//UI常量列表
		public String constantsUiList(){
			Page page = PagerFactory.createPage();
			roleService.searchConstantsUi(name,StringUtil.dealNull(pcode, ATTFrameWork.PCODE), page);
			setAttr(ATTPage.PAGE_LIST,page);
			return SUCCESS;
		}
		//UI常量保存修改
		public String suUiConts(){
			ConstantUi contsUi = null;
			if(!StringUtil.isEmpty(id)){
				contsUi = roleService.getObject(id, ConstantUi.class);
			}
			setPcode(StringUtil.dealNull(pcode, ATTFrameWork.PCODE));
			if(contsUi!=null){
				roleService.updateConsUi(cu, contsUi,imgFile,imgFileFileName);
			}else{
				roleService.saveConsUi(cu,pcode,imgFile,imgFileFileName);
			}
			return SUCCESS;
		}
		
	
	public String searchSysuser(){
		Page page = PagerFactory.createPage();
		roleService.searchSysuser(page, user);
		setAttr(ATTPage.PAGE_LIST,page);
		return SUCCESS;
	}
	public String toSysuser(){
		SysUser user = roleService.getObject(id, SysUser.class);
		List<RoleInfo> list = roleService.queryRoleInfo();
		setAttr("user", user);
		setAttr(ATTFrameWork.LIST, list);
		return SUCCESS;
	}
	public String suSysuser(){
		if(user!=null){
			SysUser u = roleService.getObject(id, SysUser.class);
			if(u!=null){
				u.setUserName(user.getUserName());
				u.setNickName(user.getNickName());
				u.setRoleInfo(user.getRoleInfo());
				roleService.updateObject(u);
				return SUCCESS;
			}else{
				u = roleService.loadSysuserByUserName(user.getUserName());
				if(u!=null){
					List<RoleInfo> list = roleService.queryRoleInfo();
					setAttr(ATTFrameWork.LIST, list);
					addFieldError("error", "用户"+user.getUserName()+"已存在!");
					return ERROR;
				}else{
					user.setCreateDate(new Date());
					user.setLoginTime(0);
					user.setPassword(new MD5Util().getMD5ofStr("123456"));
					roleService.saveObject(user);
					return SUCCESS;
				}
			}
		}
		
		return ERROR;
	}
	
}
